import 'dart:convert';
import 'dart:io';
import 'dart:typed_data';

import 'updater_platform_interface.dart';
import 'entity/download_record.dart';
import 'entity/download_status.dart';
import 'entity/installation_record.dart';
import 'storage.dart';

class AppUpdater {
  factory AppUpdater() {
    _singleton ??= AppUpdater._();
    return _singleton!;
  }

  AppUpdater._();

  static AppUpdater? _singleton;

  final AppUpdaterStorage<DownloadRecord> _downloadStorage =
      AppUpdaterStorage<DownloadRecord>(
    fileName: 'updater_download.json',
    readFrom: (String content) {
      final Map<String, dynamic> json =
          jsonDecode(content) as Map<String, dynamic>;
      return DownloadRecord.fromJson(json);
    },
    writeTo: (DownloadRecord data) {
      final Map<String, dynamic> json = data.toJson();
      return jsonEncode(json);
    },
  );

  final AppUpdaterStorage<InstallationRecord> _installStorage =
      AppUpdaterStorage<InstallationRecord>(
    fileName: 'updater_install.json',
    readFrom: (String content) {
      final Map<String, dynamic> json =
          jsonDecode(content) as Map<String, dynamic>;
      return InstallationRecord.fromJson(json);
    },
    writeTo: (InstallationRecord data) {
      final Map<String, dynamic> json = data.toJson();
      return jsonEncode(json);
    },
  );

  Future<String?> getPlatformVersion() {
    return AppUpdaterPlatform.instance.getPlatformVersion();
  }

  Future<int> enqueueDownload({
    required String url,
    String? localPath,
    Map<String, String>? headers,
    String? title,
    String? description,
  }) {
    return AppUpdaterPlatform.instance.enqueueDownload(
      url: url,
      localPath: localPath,
      headers: headers,
      title: title,
      description: description,
    );
  }

  Future<int> removeDownload(List<int> ids) {
    return AppUpdaterPlatform.instance.removeDownload(ids);
  }

  Future<DownloadStatus?> getDownloadStatus(int id) {
    return AppUpdaterPlatform.instance.getDownloadStatus(id);
  }

  Future<Uri?> getUriForDownloadedFile(int id) {
    return AppUpdaterPlatform.instance.getUriForDownloadedFile(id);
  }

  Future<Uint8List> calculateMD5Checksum(int id) {
    return AppUpdaterPlatform.instance.calculateMD5Checksum(id);
  }

  Future<void> requestInstallAPK(String uriString) {
    return AppUpdaterPlatform.instance.requestInstallAPK(uriString);
  }

  Future<DownloadRecord> reportDownload({
    required Object version,
    required int downloadId,
  }) {
    final DownloadRecord record = DownloadRecord(
      version: version,
      downloadId: downloadId,
    );
    return _downloadStorage.write(record).then((File file) => record);
  }

  Future<void> deleteDownloadRecord() => _downloadStorage.delete();

  Future<DownloadRecord?> get downloadRecord => _downloadStorage.read;

  Future<InstallationRecord> reportInstallation({
    required Object fromVersion,
    required Object toVersion,
  }) {
    final InstallationRecord data = InstallationRecord(
      fromVersion: fromVersion,
      toVersion: toVersion,
    );
    return _installStorage.write(data).then((File file) => data);
  }

  Future<void> deleteInstallationRecord() => _installStorage.delete();

  Future<InstallationRecord?> get installationRecord => _installStorage.read;
}
